package com.example.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.PageDto;
import com.example.common.vo.PageVo;
import com.example.common.vo.Result;
import com.example.entity.dto.TeacherDto;
import com.example.entity.vo.TeacherVo;
import com.example.service.pojo.Teacher;
import com.example.service.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "教师管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("teacher")
public class TeacherController {

    private final TeacherService teacherService;

    @GetMapping("{teacherId}")
    @Operation(summary = "根据id查询教师信息")
    public Result<TeacherVo> getTeacherById(@PathVariable Integer teacherId){
        Teacher teacher = teacherService.getById(teacherId);
        TeacherVo teacherVo = new TeacherVo();
        BeanUtils.copyProperties(teacher, teacherVo);
        return Result.success(teacherVo);
    }

    @PostMapping
    @Operation(summary = "添加教师信息")
    public Result<Boolean> addTeacher(@RequestBody TeacherDto teacherDto){
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDto, teacher);
        boolean save = teacherService.save(teacher);
        return Result.success(save);
    }

    @DeleteMapping("{teacherId}")
    @Operation(summary = "根据id删除教师信息")
    public Result<Boolean> deleteTeacherById(@PathVariable Integer teacherId){
        boolean remove = teacherService.removeById(teacherId);
        return Result.success(remove);
    }

    @PutMapping
    @Operation(summary = "修改教师信息")
    public Result<Boolean> updateTeacher(@RequestBody TeacherDto teacherDto){
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherDto, teacher);
        boolean update = teacherService.updateById(teacher);
        return Result.success(update);
    }

    @PostMapping("page")
    @Operation(summary = "分页查询教师信息")
    public Result<PageVo<TeacherVo>> getTeacherPage(@RequestBody PageDto<TeacherDto> pageDto){
        if(pageDto==null){
            return Result.clientFail("分页参数不能为空", null);
        }
        Page<Teacher> page = Page.of(pageDto.getPageNum(), pageDto.getPageSize());
        QueryWrapper<Teacher> wrapper = new QueryWrapper();
        wrapper.like("name", pageDto.getQuery().getName());
        page = teacherService.page(page, wrapper);
        List<Teacher> records = page.getRecords();
        List<TeacherVo> collect = records.stream().map(teacher -> {
            TeacherVo teacherVo = new TeacherVo();
            BeanUtils.copyProperties(teacher, teacherVo);
            return teacherVo;
        }).collect(Collectors.toList());
        PageVo<TeacherVo> pageVo = new PageVo<>(page.getTotal(), collect);
        return Result.success(pageVo);
    }

    @GetMapping("/getIdsByName/{teacherName}")
    @Operation(summary = "根据name查询教师id")
    public Result<List<Integer>> getTeacherIdsByName(@PathVariable String teacherName){
        List<Teacher> list = teacherService.lambdaQuery().select(Teacher::getId).like(Teacher::getName, teacherName).list();
        List<Integer> ids = list.stream().map(Teacher::getId).toList();

        return Result.success(ids);
    }

}
